package com.tong.embeddedvr.Mapper;

import com.tong.embeddedvr.dto.CollectionDTO;
import com.tong.embeddedvr.dto.TableReqDTO;
import com.tong.embeddedvr.entity.CollectionEntity;
import com.tong.embeddedvr.entity.UserEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description
 * @date 2020/12/27 14:11
 */
@Repository
@Mapper
public interface CollectionMapper {

    /**
     * @Description 添加一个新的作业集
     * @Author gcl
     * @Date 2020/12/27 16:00
     * @param: collectionEntity
     * @Return java.lang.Integer
     */
    @Insert("INSERT INTO collection(user_id,collection_name,collection_introduction,collection_request,collection_create_time,collection_start_time,collection_end_time,collection_is_delete) " +
            "VALUES(#{collectionEntity.userId}," +
            "#{collectionEntity.collectionName}," +
            "#{collectionEntity.collectionIntroduction}," +
            "#{collectionEntity.collectionRequest}," +
            "#{collectionEntity.collectionCreateTime}," +
            "#{collectionEntity.collectionStartTime}," +
            "#{collectionEntity.collectionEndTime}," +
            "0)")
    Integer addCollection(@Param("collectionEntity") CollectionEntity collectionEntity);

    /**
     * @Description 通用型的更细语句块, 用xml配置
     * @Author gcl
     * @Date 2020/12/27 15:20
     * @param: collectionEntity
     * @Return java.lang.Integer
     */
    Integer updateCollection(@Param("collectionEntity") CollectionEntity collectionEntity);

    /**
     * @Description
     * @Author gcl
     * @Date 2020/12/27 14:58
     * @param: userEntity
     * @Return java.util.List<com.tong.embeddedvr.entity.CollectionEntity>
     */
    @Select("SELECT * FROM collection WHERE user_id = #{userEntity.userId} AND collection_is_delete = 0")
    List<CollectionDTO> selectCollectionByTeacherId(@Param("userEntity") UserEntity userEntity);

    /**
     * @Description 根collectionID查找对应的collection信息
     * @Author gcl
     * @Date 2020/12/27 16:06
     * @param: collectionEntity
     * @Return java.util.List<com.tong.embeddedvr.entity.CollectionEntity>
     */
    @Select("SELECT * FROM collection WHERE collection_id = #{collectionEntity.collectionId} AND collection_is_delete = 0")
    CollectionDTO selectCollectionById(@Param("collectionEntity") CollectionEntity collectionEntity)throws Exception ;

    /**
     * @Description 只显示没有过期以及没有被当前用户选择的作业集
     * @Author gcl
     * @Date 2021/1/2 17:18
     * @param: userEntity  //这里使用userENtity更好,因为这里的userId是用来标识提交作业的学生,而不是collection的建立者
     * @Return com.tong.embeddedvr.dto.CollectionDTO
     */
    @Select("SELECT * FROM collection WHERE collection_is_delete=0 AND NOW() < collection.collection_end_time AND collection_id NOT IN (SELECT collection_id FROM homework WHERE user_id = #{userEntity.userId}) ORDER BY collection_id DESC")
    List<CollectionDTO> selectUnpassedCollection(@Param("userEntity") UserEntity userEntity) throws Exception;

    /**
     * @Description 依据老师的id对作业集的表查询,同时涵盖了分页,作业名的查询
     * @Author gcl
     * @Date 2021/1/1 12:52
     * @param: tableReqDTO
     * @param: userEntity
     * @Return com.tong.embeddedvr.dto.CollectionDTO
     */
    @Select("SELECT *,count(collection.collection_id) AS 'submitCount' FROM collection LEFT JOIN homework ON collection.collection_id = homework.collection_id\n" +
            "WHERE collection.user_id = #{userEntity.userId} AND collection_is_delete = 0 AND collection_name LIKE CONCAT('%', #{queryText}, '%')\n" +
            "GROUP BY collection.collection_id\n" +
            "ORDER BY collection.collection_id DESC\n" +
            "LIMIT #{start},#{pageSize}\n")
    List<CollectionDTO> selectTable(@Param("queryText") String queryText,@Param("start") Integer start,@Param("pageSize") Integer pageSize,@Param("userEntity") UserEntity userEntity) throws Exception;

    /**
     * @Description 实际使用的分页条数查询,包含了模糊匹配,由于mybatis使用函数名作为方法的唯一标识,无法实现重载
     * @Author gcl
     * @Date 2021/1/1 18:39
     * @param: queryText
 @param: userEntity
      * @Return java.lang.Integer
     */
    @Select("SELECT COUNT(*) FROM collection WHERE user_id = #{userEntity.userId} AND collection_name LIKE CONCAT('%', #{queryText}, '%')")
    Integer selectCount(@Param("queryText") String queryText,@Param("userEntity") UserEntity userEntity) throws Exception;
}
